/**
* \file pnm2vti.cpp
*
* \brief This file encompasses the PNM to VTI file format application.
* \author Shekhar S. Chandra, 2007-2009
*
* This file is part of DGV Library.
*
* DGV is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DGV is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DGV.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "vtkImageData.h"
#include "vtkPNMReader.h"
#include "vtkXMLImageDataWriter.h"

#include "DGVAliases.h"

///Function Prototypes
void processArgs(int argcount, char *argvars[], int &binary, string &prefix, int &startSlice, int &endSlice);

int main(int argc, char *argv[])
{
    int start = 0, end = 10, tmp = 0;
    string filePrefix, tmpStr1, tmpStr2;
    bool writeBinary = false, isPrefix = true;

    cout << ">| Portable aNy Map (PNM) Format to VTK Image Format (VTI)" << endl;
    cout << ">| PNM formats have the extension *.pbm *.pgm *.pnm *.ppm" << endl;
    cout << ">| It may read in a single file or a stack like: <FileName>.pgm.[Number]" << endl;
    cout << ">| Copyright Shekhar Chandra, 2007-2009." << endl;

    ///Process arguments
	processArgs(argc,argv,tmp,filePrefix,start,end);

	if(start == 0 && end == 0)
        isPrefix = false;
    if(tmp == 1)
        writeBinary = true;

    cout << ">| File to be read";
    if(isPrefix)
    {
        cout << " with prefix: " << filePrefix << endl;
        cout << ">| Slices to be read: " << start << "-" << end << endl;
    }
    else
        cout << " with name: " << filePrefix << endl;
    cout << ">| VTI file written to : " << filePrefix+".vti";
    if(writeBinary)
        cout << " (Binary)" << endl;
    else
        cout << " (ASCII)" << endl;
    cout << ">| You may wish to rename this" << endl;

    ///Open PNM File/Stack
    vtkPNMReader* reader = vtkPNMReader::New();
    tmpStr1 = filePrefix + "." + numToString(start);
    tmpStr2 = filePrefix + "." + numToString(end);
    if( (reader->CanReadFile(tmpStr1.c_str())
        && reader->CanReadFile(tmpStr2.c_str()))
        || reader->CanReadFile(filePrefix.c_str()) )
    {
        reader->SetDebug(true);
        if(isPrefix)
        {
            int bounds[6] = {0};

            bounds[4] = start;
            bounds[5] = end;

            reader->SetDataExtent(bounds);
            reader->SetFilePrefix(filePrefix.c_str());
            reader->Update();
        }
        else
        {
            reader->SetFileName(filePrefix.c_str());
            reader->Update();
        }

        vtkImageData *data = reader->GetOutput();

        ///Output to VTK XML Image (VTI) file
        cout << ">| Writing VTI File ";
        vtkXMLImageDataWriter *XMLwriter = vtkXMLImageDataWriter::New();
            filePrefix += ".vti";
            XMLwriter->SetFileName(filePrefix.c_str());
            if(writeBinary)
                XMLwriter->SetDataModeToBinary();
            else
                XMLwriter->SetDataModeToAscii();
            XMLwriter->SetInput(data);
            XMLwriter->Write();
        cout << "Done." << endl;

        XMLwriter->Delete();
    }
    reader->Delete();

    return EXIT_SUCCESS;
}

///Function Bodies
void processArgs(int argcount, char *argvars[], int &binary, string &prefix, int &startSlice, int &endSlice)
{
    switch(argcount)
    {
        case 2:
            prefix = argvars[1];
            binary = 0;
            startSlice = 0;
            endSlice = 0;
            break;
        case 3:
            prefix = argvars[1];
            binary = atoi(argvars[2]);
            startSlice = 0;
            endSlice = 0;
            break;
        case 5:
            prefix = argvars[1];
            binary = atoi(argvars[2]);
            startSlice = atoi(argvars[3]);
            endSlice = atoi(argvars[4]);
            break;
        default:
            printf(">| Usage: %s <File Name/Prefix> [Write Binary?] [Start Bound] [End Bound]\n",argvars[0]);
            printf(">| <File Name/Prefix> is required and the rest are optional.\n");
            printf(">| [Write Binary] should be a Boolean.");
            exit(EXIT_FAILURE);
    }
}
